package gui;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.Dimension;
import javax.swing.JPanel;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;

import javax.swing.JButton;
import control.informes.ModeloInformeEstado;

import java.awt.Insets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComboBox;

import datos.Tarea;

@SuppressWarnings("serial")
public class InterfazInformeEstado extends JDialog {

	private ModeloInformeEstado modelo = null;

	private JPanel jContentPane = null;
	private JLabel jLEstado = null;
	private JPanel jPBotones = null;
	private JButton jBCancelar = null;
	private JButton jBGenerar = null;
	private JPanel jPEstado = null;
	private JComboBox jCBEstado = null;

	private List<Tarea> lista = null;

	/**
	 * This method initializes
	 * 
	 */
	public InterfazInformeEstado(JFrame frame, List<Tarea> lista) {
		super();
		this.lista = lista;
		initialize(frame);
		setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize(JFrame frame) {
		this.setSize(new Dimension(358, 142));
		this.setTitle("Generar informe por estado");
		this.setModal(true);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(frame);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 2;
			gridBagConstraints12.fill = GridBagConstraints.BOTH;
			gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.weighty = 1.0;
			gridBagConstraints12.gridy = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridwidth = 4;
			gridBagConstraints11.gridy = 5;
			jLEstado = new JLabel();
			jLEstado.setText("Seleccionar Estado");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getJPBotones(), gridBagConstraints11);
			jContentPane.add(getJPFechas(), gridBagConstraints12);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPBotones
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPBotones() {
		if (jPBotones == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(10);
			flowLayout.setVgap(5);
			jPBotones = new JPanel();
			jPBotones.setLayout(flowLayout);
			jPBotones.add(getJBGenerar(), null);
			jPBotones.add(getJBCancelar(), null);
		}
		return jPBotones;
	}

	/**
	 * This method initializes jBCancelar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBCancelar() {
		if (jBCancelar == null) {
			jBCancelar = new JButton();
			jBCancelar.setText("Cancelar");
			jBCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
		}
		return jBCancelar;
	}

	/**
	 * This method initializes jBGenerar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJBGenerar() {
		if (jBGenerar == null) {
			jBGenerar = new JButton();
			jBGenerar.setText("Generar");
			jBGenerar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String estado = (String) getJCBEstado().getSelectedItem();
					if (estado.equals("")) {
						JOptionPane.showMessageDialog(getJContentPane(), "No se ha seleccionado un estado");
						return;
					}

					List<Tarea> l = new ArrayList<Tarea>();
					Iterator<Tarea> i = lista.iterator();
					while (i.hasNext()) {
						Tarea tarea = i.next();
						if (tarea.getEstado().equals(estado))
							l.add(tarea);
					}
					modelo = new ModeloInformeEstado(l, estado);
					setVisible(false);
				}
			});
		}
		return jBGenerar;
	}

	public ModeloInformeEstado modelo() {
		return modelo;
	}

	/**
	 * This method initializes jPFechas
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPFechas() {
		if (jPEstado == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridx = -1;
			gridBagConstraints1.gridy = -1;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			jPEstado = new JPanel();
			jPEstado.setLayout(new GridBagLayout());
			jPEstado.add(jLEstado, gridBagConstraints1);
			jPEstado.add(getJCBEstado(), gridBagConstraints);
		}
		return jPEstado;
	}

	/**
	 * This method initializes jCBEstado
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJCBEstado() {
		if (jCBEstado == null) {
			jCBEstado = new JComboBox(Tarea.ESTADOS);
		}
		return jCBEstado;
	}

} // @jve:decl-index=0:visual-constraint="138,103"
